/*
 * ImportDialogWidget.fx
 *
 * Created on 10-Feb-2009, 23:32:07
 */

package widgets;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.ext.swing.SwingTextField;
import javafx.ext.swing.SwingLabel;
import javafx.ext.swing.SwingHorizontalAlignment;
import javafx.scene.text.*;
import javafx.scene.paint.Color;
import widgets.NextStepButtonWidget;
import javafx.ext.swing.SwingButton;
import javafx.scene.input.MouseEvent;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.Interpolator;
import logic.Importer;

/**
 * @author beol
 */

public class ImportDialogWidget extends CustomNode{

    public var x:Number;
    public var y:Number;

    var node:Group = Group {
    }

    var background:Rectangle = Rectangle {
        translateX: x - 8;
        translateY: y - 4;
        width: 220
        height: 148
        arcWidth: 30
        arcHeight: 30
        stroke: Color.BLACK
        fill: Color.LIGHTGREY
    }

    var moiKrugLogo:ImageView = ImageView {
        image: Image {
            url: "{__DIR__}moi_krug.png"
        }

        translateX: x+3
        translateY: y + 4
        //scaleX: 0.8;
        //scaleY: 0.8;
    }

    var labelFont = Font.font("s", FontWeight.BOLD, 16);

    var login: SwingTextField = SwingTextField {
        font: labelFont
        translateX: x + 7
        translateY: y + 50
        columns: 6
        text: "ваш логин"
        focusable: false
        editable: true
        horizontalAlignment: SwingHorizontalAlignment.CENTER;
        background: Color.WHITE;
        borderless: true
        foreground: Color.BLACK;

        onMouseEntered: function( e: MouseEvent ):Void {
            login.cursor = javafx.scene.Cursor.TEXT;
        }

        onMouseExited: function( e: MouseEvent ):Void {
            login.cursor = javafx.scene.Cursor.DEFAULT;
        }
        
    }

    var textFieldBackground:Rectangle = Rectangle {
        translateX: x + 3,
        translateY: y + 47
        width: 102,
        height: 26
        fill: Color.WHITE
        arcHeight: 10
        arcWidth: 10
        stroke: Color.BLACK

        strokeWidth: 1
}

    var loginPostfix: SwingLabel = SwingLabel {
        font: labelFont;
        translateX: x + 107;
        translateY: y + 50;

        text: ".moikrug.ru"
    }

    var syncButton: ButtonWidget = ButtonWidget {
        x: x + 2
        y: y + 82
        height: 22
        width: 200
        text: "ипортировать данные"
        offsetX: 12
        offsetY: 16
    }

    var skipButton: ButtonWidget = ButtonWidget {
        x: x + 2
        y: y + 112
        width: 200
        height: 22
        text: "ввести данные вручную"
        offsetX: 7
        offsetY: 16
    }

    var isSyncPressed = bind syncButton.isPressed on replace do {
        if (isSyncPressed) {
            syncButton.isPressed = false;
            showImportDesk.playFromStart();

        }
    }

    var importDeskOpacity: Number = 0.0;

    var showImportDesk: Timeline = Timeline {
        repeatCount: 1
        interpolate: true
        keyFrames: [
            KeyFrame {
                time: 0s
                values: importDeskOpacity => 0.0
                action: function() {
                    importDesk.visible = true;
                }
            },
            KeyFrame {
                time: 0.5s
                values: importDeskOpacity => 1.0 tween Interpolator.LINEAR
                action: function() {
                    importer.getData("atmakin");

                }

            }
        ]
    }

    var importDesk: ImportDeskWidget = ImportDeskWidget {
        x: x - 8,
        y: y + 160
        opacity: bind importDeskOpacity
        visible: false
    }

    def importer:Importer = new Importer;

    protected override function create(): Node {

        node.content = [background,
                        moiKrugLogo,
                        textFieldBackground,
                        login,
                        loginPostfix,
                        syncButton,
                        skipButton,
                        importDesk];
        return node;
    }
}
